package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            /*for (long rid : ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }*/
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }

    }

    @Override
    public void delete(Long id) {
        // 在删除数据之前 删除用户绑定的角色关系
        employeeMapper.deleteEmployeeBindRole(id);

        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {

        employeeMapper.updateByPrimaryKey(employee);


        // 在更新数据之前 删除之前的角色绑定 重新绑定角色关系
        employeeMapper.deleteEmployeeBindRole(employee.getId());


        if (ids != null && ids.length > 0) {
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    public PageInfo<Employee> query(EmployeeQueryObject qo){
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo(employees);


//        int count = employeeMapper.selectForCount(qo);
//        if(count < 0){
//            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(), Collections.emptyList(),count);
//        }else{
//            List<Employee> employees = employeeMapper.selectForList(qo);
//            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),employees,count);
//        }
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);
        if(employee == null){
            throw new RuntimeException("账号和密码不匹配");
        }
        return employee;
    }

    @Override
    public void updatePwd(Long id, String password) {
        employeeMapper.updateByUsernameAndPassword(id,password);
//        Employee employee =
//        if(employee == null){
//            throw new RuntimeException("修改密码失败!");
//        }
//        return employee;
    }

    /**
     * 删除选中状态的列表数据
     * @param ids 选中状态的 id 数组
     */
    @Override
    public void deleteCheckedList(Long[] ids) {
        employeeMapper.deleteCheckedList(ids);
    }

    @Override
    public Employee selectByName(String name) {
        Employee employee = employeeMapper.selectByUsername(name);
        return employee;
    }
}
